<template>
  <div>
    <h1>品牌管理</h1>
  </div>
  <div>
    <el-form :inline="true" :model="searForm" class="demo-form-inline">
      <el-form-item label="品牌名称">
        <el-input
          v-model="searForm.keyWord"
          placeholder="请输入品牌名称"
          clearable
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">搜索</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div>
    <el-button type="primary" @click="handleAddBrand">添加品牌</el-button>
  </div>
  <div>
    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="brandId" label="ID" />
      <el-table-column prop="brandName" label="品牌名称" />
      <el-table-column prop="useCount" label="使用数量" />
      <el-table-column prop="brandTop" label="排序" />
      <el-table-column prop="brandStatus" label="状态">
        <template #default="scope">
          <el-switch
            v-model="scope.row.brandStatus"
            inline-prompt
            active-text="显示"
            :active-value="0"
            inactive-text="隐藏"
            :inactive-value="1"
            @change="onCheckStatus(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column label="操作" />
    </el-table>
  </div>
  <div style="margin-top: 20px">
    <el-pagination
      v-model:current-page="searForm.pageNum"
      v-model:page-size="searForm.pageSize"
      :page-sizes="[2, 4, 6, 8]"
      :background="true"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
  <div>
    <el-dialog v-model="dialogFormVisible" title="添加品牌" width="500">
      <el-form ref="ruleFormRef" :model="form" :rules="rules">
        <el-form-item
          label="品牌名称"
          prop="brandName"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.brandName" autocomplete="off" />
        </el-form-item>

        <el-form-item
          label="排序"
          prop="brandTop"
          :label-width="formLabelWidth"
        >
          <el-input-number v-model="form.brandTop" :min="1" />
        </el-form-item>

        <el-form-item
          label="状态"
          prop="brandStatus"
          :label-width="formLabelWidth"
        >
          <el-radio-group v-model="form.brandStatus">
            <el-radio :value="0">显示</el-radio>
            <el-radio :value="1">隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">放弃</el-button>
          <el-button type="primary" @click="submitForm(ruleFormRef)">
            提交
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from "vue";
import { getBrandList, addBrand, checkBrandStatus } from "../api/api";
import { ElMessage } from "element-plus";
import type { FormInstance, FormRules } from "element-plus";

const total = ref(0);
const tableData = ref([]);
const searForm = ref({
  pageNum: 1,
  pageSize: 2,
  keyWord: null,
});
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";

const form = ref({
  brandName: null,
  brandStatus: null,
  brandTop: null,
  createBy: null,
  createTime: null,
  updateBy: null,
  updateTime: null,
  useCount: null,
});
const ruleFormRef = ref<FormInstance>();
const rules = reactive<FormRules<any>>({
  brandName: [{ required: true, message: "请输入品牌名称", trigger: "blur" }],
  brandTop: [
    { required: true, message: "请输入排序", trigger: "blur" },
    // { min: 3, max: 5, message: "Length should be 3 to 5", trigger: "blur" },
  ],

  brandStatus: [
    {
      required: true,
      message: "请选择状态",
      trigger: "change",
    },
  ],
});

const loadList = () => {
  getBrandList(searForm.value).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};

const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searForm.value.pageSize = val;
  loadList();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searForm.value.pageNum = val;
  loadList();
};

const handleAddBrand = () => {
  dialogFormVisible.value = true;
};

const onsubmitAdd = () => {
  addBrand(form.value).then((res) => {
    if (res.data.code == 200) {
      dialogFormVisible.value = false;
      ElMessage.success("添加成功");
      loadList();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

//校验后提交表单
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
      //检验通过
      onsubmitAdd();
    } else {
      //校验失败
      console.log("error submit!", fields);
    }
  });
};

//更新数据库的状态
const onCheckStatus = (row: any) => {
  checkBrandStatus(row).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("成功更新状态");
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

const onSubmit = () => {
  console.log("submit!");
  loadList();
};

onMounted(() => {
  loadList();
});
</script>
